var app = getApp()
Page({
  data: {
  },
  onLoad() {
    let user=wx.getStorageSync('user')
    let openid=wx.getStorageSync('openid') 
    app.globalData.userInfo=user
    app.globalData.openID=openid
     if(user){
      wx.switchTab({
        url: '/pages/home/home'
      })
     }
  },
  login() {
    wx.getUserProfile({
      desc: '用于完善用户资料',
      success: (res) => {
        let user = res.userInfo;
        wx.setStorageSync('user', user);
        
        wx.login({
          success: (res) => {   
            // 在 wx.login() 的回调函数中获取 openID
            wx.request({
              url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx631b373763161d5f&secret=a3c9f798be751e07bf54cb7755a551a8&js_code=' + res.code + '&grant_type=authorization_code',
              success: (res) => {
                let openid = res.data.openid;
                wx.setStorageSync('openid', openid);
                app.globalData.openID=res.data.openid
              }
            });
          }
        });
        
        app.globalData.userInfo=res.userInfo
        console.log(app.globalData.userInfo)
        wx.switchTab({
          url: '/pages/home/home'
        })

      }
    });
    
  }
})
